#include "SessionNotebook.h"
#include "SessionPanel.h"
#include "MessengerPanel.h"
#include "FileTransferPanel.h"

#include "MsnDef.h"

int ID_SESSION_NOTEBOOK = wxNewId();

BEGIN_EVENT_TABLE(SessionNotebook,NotebookCtrl)

END_EVENT_TABLE()


SessionNotebook::SessionNotebook(wxWindow *parent)
	: NotebookCtrl(parent, ID_SESSION_NOTEBOOK, wxDefaultPosition, wxDefaultSize, wxNB_TOP)
{
	SetImageList(&MsnDef::ICON_LIST);

	SetBackgroundColour(*wxWHITE);
	//SetBackgroundColour(*wxLIGHT_GREY);
	//SetBackgroundColour(wxColour(208,227,232));
}
            
void SessionNotebook::SelectPanel(wxWindow* panel)
{
    for(unsigned int i=0; i < GetPageCount(); i++){
        if(panel == GetPage(i)){
            SetSelection(i);
            return;
        }
    }
}

SessionPanel* SessionNotebook::FindPanelByPassport(const wxString& passport)
{
    for(unsigned int i=0; i < GetPageCount(); i++){
		SessionPanel* panel = (SessionPanel*)GetPage(i);

		if(panel->IsForContact(passport))
			return panel;
    }	

	return NULL;
}

void SessionNotebook::ClosePage(int id)
{
	DEBUG(_T("session notebook close page"));

	SessionPanel* panel = (SessionPanel*)GetPage(id);

	if(panel->ftPanel->HasLiveItem(panel->GetSession())){
		DEBUG(_T("Live panel in file transfer window"));
		wxMessageDialog dialog(NULL, _("File transferring is not done, continue?"), _("Close Conversation"), wxYES_NO);

		switch(dialog.ShowModal()){	
			case wxID_NO:
				return;
			case wxID_YES:
				panel->ftPanel->CancelLiveItem(panel->GetSession());
		}
	}

	NotebookCtrl::ClosePage(id);
}

void SessionNotebook::ClosePageAll()
{
	//get any session panel
	SessionPanel* panel = (SessionPanel*)GetPage(0);

	if(panel->ftPanel->HasLiveItemAll()){
		DEBUG(_T("Live panel in file transfer window"));
		wxMessageDialog dialog(NULL, _("File transferring is not done, continue?"), _("Close Conversation"), wxYES_NO);

		switch(dialog.ShowModal()){	
			case wxID_NO:
				return;
			case wxID_YES:
				panel->ftPanel->CancelLiveItemAll();
		}
	}

	NotebookCtrl::ClosePageAll();
}

void SessionNotebook::ClosePageOthers(int id)
{

	SessionPanel* panel = (SessionPanel*)GetPage(id);

	if(panel->ftPanel->HasLiveItemOthers(panel->GetSession())){
		DEBUG(_T("Live panel in file transfer window"));
		wxMessageDialog dialog(NULL, _("File transferring is not done, continue?"), _("Close Conversation"), wxYES_NO);

		switch(dialog.ShowModal()){	
			case wxID_NO:
				return;
			case wxID_YES:
				panel->ftPanel->CancelLiveItemOthers(panel->GetSession());
		}

	}

	NotebookCtrl::ClosePageOthers(id);
}
